package com.flappySeeker.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.flappySeeker.common.BaseDaoImp;
import com.flappySeeker.model.TbUserinfo;

@Controller
@RequestMapping("/cmd")
public class CommandAction {
	@Autowired
	private BaseDaoImp baseDaoImp;
	
	public BaseDaoImp getBaseDaoImp() {
		return baseDaoImp;
	}
	public void setBaseDaoImp(BaseDaoImp baseDaoImp) {
		this.baseDaoImp = baseDaoImp;
	}
	
	/**收费测试一转发测试二的方法
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/cmdurl")
	public void cmdUrl(HttpServletRequest request, HttpServletResponse response) throws IOException{
		try {
			request.getRequestDispatcher("../jsp/test/stage_2.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**收费开始测试入口方法
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws SQLException 
	 */
	@RequestMapping(value="/cmdurl2sf")
	public void cmdUrl2SF(TbUserinfo user,HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String suc = "0", msg = "";
		Map<String,Object> list = baseDaoImp.sqlformap("select userid,username,usertel,verificode,vsmscode,resultsurl from tb_userinfo where " +
				"username = '"+user.getUsername()+"' and " +
				"usertel = '"+user.getUsertel()+"'");
		if(list!=null){
			if(!list.get("vsmscode").equals(user.getVsmscode().trim())){
				msg = "手机验证码不正确,请确认后重新输入或重新发送验证码。";
				out.print("[{'suc':'"+suc+"','msg':'"+msg+"'}]");
				return;
			} else if(!list.get("verificode").equals(user.getVerificode().replace(" ", ""))){
				msg = "序列号不正确,请确认后重新输入或点击购买按钮立即购买。";
				out.print("[{'suc':'"+suc+"','msg':'"+msg+"'}]");
				return;
			} else {
				request.getSession().setAttribute("cmdurl2sf", "sf"); //验证正确则记录会话
				request.getSession().setAttribute("sf_username",list.get("username"));
				System.out.println(list.get("userid")+"+++++++++++");
				request.getSession().setAttribute("sf_userid", list.get("userid"));
				if(list.get("resultsurl")!=null&&!list.get("resultsurl").equals("")){ //已测试过一阶段
					request.getSession().setAttribute("isteststatus", "tested_one"); //已测试过一阶段会话
					suc = "2";
					out.print("[{'suc':'"+suc+"'}]");
					return;
				} else {
					suc = "1";
					out.print("[{'suc':'"+suc+"'}]");
					return;
				}
			}
			
		}else{
			msg = "系统中无对应的用户和手机号信息。";
			out.print("[{'suc':'"+suc+"','msg':'"+msg+"'}]");
			return;
		}
	}
	
	public static void main(String args[]){}
}
